package com.dominator.controller.api;

import com.alibaba.fastjson.JSON;
import com.dominFramework.core.typewrap.Dto;
import com.dominFramework.core.typewrap.Dtos;
import com.dominFramework.core.utils.JsonUtils;
import com.dominator.enums.ReqEnums;
import com.dominator.service.*;
import com.dominator.utils.api.ApiMessage;
import com.dominator.utils.api.ApiMessageUtil;
import com.dominator.utils.exception.ApiException;
import com.dominator.utils.network.IpUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;

@CrossOrigin
@RestController
@RequestMapping("/api/v1/common")
@Slf4j
public class ApiCommonController {

    @Autowired
    HttpServletRequest request;

    @Autowired
    ApiCommonService apiCommonService;

    @Autowired
    ApiPointTicketService apiPointTicketService;

    @Autowired
    ApiWzlService apiWzlService;

    @Autowired
    ApiUserService apiUserService;

    @Autowired
    ApiBankService apiBankService;

    @Autowired
    ApiLotteryService apiLotteryService;

    @Autowired
    ApiEntranceTicketService apiEntranceTicketService;

    @Autowired
    ApiShopService apiShopService;

    @Autowired
    ApiBigRewardService apiBigRewardService;



    /**
     * 获取openid
     *
     * @param paramStr
     * @return
     */
    @PostMapping("/getWxUserOpenId")
    public ApiMessage getWxUserOpenid(@RequestParam String paramStr) {
        ApiMessage msg;
        try {
            msg = apiCommonService.getWxUserOpenId(JsonUtils.toDto(paramStr));
        } catch (ApiException e) {
            msg = new ApiMessage(e.getCode(), e.getMessage());
        } catch (Exception e) {
            msg = new ApiMessage(ReqEnums.SYS_ERROR);
            e.printStackTrace();
        }
        return msg;

    }

    /**
     * 微信下单weixinCreateOrder
     *
     * @param paramStr
     * @return
     */
    @PostMapping("/weixinCreateOrder")
    public ApiMessage weixinCreateOrder(@RequestParam String paramStr) {
        ApiMessage msg;
        try {
            Dto dto = JsonUtils.toDto(paramStr);
            dto.put("ip", IpUtils.getIpAddr(request));
            msg = apiCommonService.weixinCreateOrder(dto);
        } catch (ApiException e) {
            msg = new ApiMessage(e.getCode(), e.getMessage());
        } catch (Exception e) {
            msg = new ApiMessage(ReqEnums.SYS_ERROR);
            e.printStackTrace();
        }
        return msg;

    }

    /**
     * 微信回调
     *
     * @param
     * @return
     */
    @GetMapping("/wexinCallBack")
    public ApiMessage wexinCallBack() {
        ApiMessage msg;
        try {
            msg = apiCommonService.wexinCallBack(request);
        } catch (ApiException e) {
            msg = new ApiMessage(e.getCode(), e.getMessage());
        } catch (Exception e) {
            msg = new ApiMessage(ReqEnums.SYS_ERROR);
            e.printStackTrace();
        }
        return msg;

    }
    /**
     * 微信回调
     *
     * @param
     * @return
     */
    @PostMapping("/wexinCallBack")
    public ApiMessage wexinCallBackPost() {
        return wexinCallBack();
    }



    /**
     * 查询用户
     *
     * @param paramStr
     * @return
     */
    @PostMapping("/getUser")
    public ApiMessage getUser(@RequestParam String paramStr) {
        ApiMessage msg;
        try {
            msg = apiUserService.getUser(JsonUtils.toDto(paramStr));
        } catch (ApiException e) {
            msg = new ApiMessage(e.getCode(), e.getMessage());
        } catch (Exception e) {
            msg = new ApiMessage(ReqEnums.SYS_ERROR);
            e.printStackTrace();
        }
        return msg;

    }


    /**
     * 上传图片
     *
     * @param uploadPicture
     * @return
     */
    @PostMapping("/uploadPicture")
    public ApiMessage uploadPicture(@RequestParam(value = "uploadPicture") MultipartFile uploadPicture) {
        ApiMessage msg;
        try {
            msg = apiCommonService.uploadPicture(uploadPicture);
        } catch (ApiException e) {
            msg = new ApiMessage(e.getCode(), e.getMessage());
        } catch (Exception e) {
            msg = new ApiMessage(ReqEnums.SYS_ERROR);
            e.printStackTrace();
        }
        return msg;
    }

    /**
     * 首页
     *
     * @param
     * @return
     */
    @GetMapping("/homepage")
    public ApiMessage homepage() {
        ApiMessage msg;
        try {
            msg = apiCommonService.homepage();
        } catch (ApiException e) {
            msg = new ApiMessage(e.getCode(), e.getMessage());
        } catch (Exception e) {
            msg = new ApiMessage(ReqEnums.SYS_ERROR);
            e.printStackTrace();
        }
        return msg;
    }

    /**
     * 获取图像验证码
     *
     * @param
     * @return
     */
    @PostMapping("/getPicVerifyCode")
    public ApiMessage getPicVerifyCode() {
        ApiMessage msg;
        try {
            msg = apiCommonService.getPicVerifyCode();
        } catch (ApiException e) {
            msg = new ApiMessage(e.getCode(), e.getMessage());
        } catch (Exception e) {
            msg = new ApiMessage(ReqEnums.SYS_ERROR);
            e.printStackTrace();
        }
        return msg;
    }

    /**
     * 批量发码
     *
     * @return
     */
    @PostMapping(value = "/batchCreate")
    public ApiMessage batchCreate(@RequestParam String paramStr) {
        ApiMessage msg = new ApiMessage();
        try {
            Dto dto = JsonUtils.toDto(paramStr);
            msg = apiPointTicketService.batchCreate(dto);
        } catch (ApiException e) {
            msg.setCode(e.getCode());
            msg.setMessage(e.getMessage());
        } catch (Exception e) {
            log.error(e.getMessage());
            msg = new ApiMessage(ReqEnums.SYS_ERROR);
            e.printStackTrace();
        } finally {
            return msg;
        }

    }

    /**
     * 单码核销
     *
     * @return
     */
    @PostMapping(value = "/verification")
    public ApiMessage verification(@RequestParam String paramStr) {
        ApiMessage msg = new ApiMessage();
        try {
            Dto dto = JsonUtils.toDto(paramStr);
            msg = apiPointTicketService.verification(dto);
        } catch (ApiException e) {
            msg.setCode(e.getCode());
            msg.setMessage(e.getMessage());
        } catch (Exception e) {
            log.error(e.getMessage());
            msg = new ApiMessage(ReqEnums.SYS_ERROR);
            e.printStackTrace();
        } finally {
            return msg;
        }

    }

    /**
     * 单码核销
     *
     * @return
     */
    @PostMapping(value = "/getWxConfig")
    public ApiMessage getWxConfig(@RequestParam String paramStr) {
        ApiMessage msg = new ApiMessage();
        try {
            Dto dto = JsonUtils.toDto(paramStr);
            msg = apiCommonService.getWxConfig(dto);
        } catch (ApiException e) {
            msg.setCode(e.getCode());
            msg.setMessage(e.getMessage());
        } catch (Exception e) {
            log.error(e.getMessage());
            msg = new ApiMessage(ReqEnums.SYS_ERROR);
            e.printStackTrace();
        } finally {
            return msg;
        }

    }

    /**
     * 兑换券列表
     *
     * @return
     */
    @PostMapping(value = "/getPointTicketList")
    public ApiMessage getPointTicketList(@RequestParam String paramStr) {
        ApiMessage msg = new ApiMessage();
        try {
            Dto dto = JsonUtils.toDto(paramStr);
            msg = apiPointTicketService.getPointTicketList(dto);
        } catch (ApiException e) {
            msg.setCode(e.getCode());
            msg.setMessage(e.getMessage());
        } catch (Exception e) {
            log.error(e.getMessage());
            msg = new ApiMessage(ReqEnums.SYS_ERROR);
            e.printStackTrace();
        } finally {
            return msg;
        }

    }

    /**
     * 彩票二维码查询
     *
     * @param
     * @return
     */
    @PostMapping(value = "/lotteryQrcodeQuery")
    public ApiMessage lotteryQrcodeQuery() {
        ApiMessage msg = new ApiMessage();
        try {
            msg = apiWzlService.lotteryQrcodeQuery();
        } catch (ApiException e) {
            msg.setCode(e.getCode());
            msg.setMessage(e.getMessage());
        } catch (Exception e) {
            log.error(e.getMessage());
            msg = new ApiMessage(ReqEnums.SYS_ERROR);
            e.printStackTrace();
        } finally {
            return msg;
        }

    }

    /**
     * 获取彩票联合登录ID
     *
     * @param paramStr
     * @return
     */
    @PostMapping(value = "/getLotteryUnionId")
    public ApiMessage getUnionId(@RequestParam String paramStr) {
        ApiMessage msg = new ApiMessage();
        try {
            Dto dto = JsonUtils.toDto(paramStr);
            msg = apiWzlService.getUnionId(dto);
        } catch (ApiException e) {
            msg.setCode(e.getCode());
            msg.setMessage(e.getMessage());
        } catch (Exception e) {
            log.error(e.getMessage());
            msg = new ApiMessage(ReqEnums.SYS_ERROR);
            e.printStackTrace();
        }
        return msg;

    }

    /**
     * 彩票二维码发送
     *
     * @param paramStr
     * @return
     */
    @PostMapping(value = "/lotteryQrcodeSend")
    public ApiMessage lotteryQrcodeSend(@RequestParam String paramStr) {
        ApiMessage msg = new ApiMessage();
        try {
            Dto dto = JsonUtils.toDto(paramStr);
            msg = apiWzlService.lotteryQrcodeSend(dto);
        } catch (ApiException e) {
            msg.setCode(e.getCode());
            msg.setMessage(e.getMessage());
        } catch (Exception e) {
            log.error(e.getMessage());
            msg = new ApiMessage(ReqEnums.SYS_ERROR);
            e.printStackTrace();
        } finally {
            return msg;
        }

    }

    /**
     * 获取微信支持的银行卡列表
     *
     * @return
     */
    @GetMapping(value = "/getWechatBankList")
    public ApiMessage getWechatBankList() {
        ApiMessage msg = new ApiMessage();
        try {
            msg = apiCommonService.getWechatBankList();
        } catch (ApiException e) {
            msg.setCode(e.getCode());
            msg.setMessage(e.getMessage());
        } catch (Exception e) {
            log.error(e.getMessage());
            msg = new ApiMessage(ReqEnums.SYS_ERROR);
            e.printStackTrace();
        } finally {
            return msg;
        }

    }


    /**
     * 添加银行卡
     *
     * @param paramStr
     * @return
     */
    @PostMapping("/addBank")
    public ApiMessage addBank(@RequestParam String paramStr) {
        ApiMessage msg;
        try {
            msg = apiBankService.addBank(JsonUtils.toDto(paramStr));
        } catch (ApiException e) {
            msg = new ApiMessage(e.getCode(), e.getMessage());
        } catch (Exception e) {
            msg = new ApiMessage(ReqEnums.SYS_ERROR);
            e.printStackTrace();
        }
        return msg;

    }

    /**
     * 获取用户银行卡列表
     *
     * @param paramStr
     * @return
     */
    @PostMapping("/getUserBankList")
    public ApiMessage getUserBankList(@RequestParam String paramStr) {
        ApiMessage msg;
        try {
            msg = apiBankService.getUserBankList(JsonUtils.toDto(paramStr));
        } catch (ApiException e) {
            msg = new ApiMessage(e.getCode(), e.getMessage());
        } catch (Exception e) {
            msg = new ApiMessage(ReqEnums.SYS_ERROR);
            e.printStackTrace();
        }
        return msg;

    }

    /**
     * 删除用户银行卡
     *
     * @param paramStr
     * @return
     */
    @PostMapping("/deleteBank")
    public ApiMessage deleteBank(@RequestParam String paramStr) {
        ApiMessage msg;
        try {
            msg = apiBankService.deleteBank(JsonUtils.toDto(paramStr));
        } catch (ApiException e) {
            msg = new ApiMessage(e.getCode(), e.getMessage());
        } catch (Exception e) {
            msg = new ApiMessage(ReqEnums.SYS_ERROR);
            e.printStackTrace();
        }
        return msg;

    }


    /**
     * 上传图片
     *
     * @param orcPicture
     * @return
     */
    @PostMapping("/ocr")
    public ApiMessage ocr(@RequestParam(value = "orcPicture") MultipartFile orcPicture) {
        ApiMessage msg;
        try {
            msg = apiCommonService.ocrPicture(orcPicture);
        } catch (ApiException e) {
            msg = new ApiMessage(e.getCode(), e.getMessage());
        } catch (Exception e) {
            msg = new ApiMessage(ReqEnums.SYS_ERROR);
            e.printStackTrace();
        }
        return msg;
    }




    /**
     * 彩票返奖
     * @return
     */
    @PostMapping(value = "/betBonusMoney")
    public ApiMessage betBonusMoney(@RequestParam String paramStr) {
        ApiMessage msg = new ApiMessage();
        try {
            Dto dto = JsonUtils.toDto(paramStr);
            msg = apiLotteryService.betBonusMoney(dto);
        } catch (ApiException e) {
            msg.setCode(e.getCode());
            msg.setMessage(e.getMessage());
        } catch (Exception e) {
            log.error(e.getMessage());
            msg = new ApiMessage(ReqEnums.SYS_ERROR);
            e.printStackTrace();
        } finally {
            return msg;
        }

    }

    /**
     * 微信上传小票，传七牛
     *
     * @param
     * @return
     */
    @PostMapping(value = "/ocrWeixinTicket")
    public ApiMessage ocrWeixinTicket(@RequestParam String paramStr) {
        ApiMessage msg;
        try {
            Dto dto= JsonUtils.toDto(paramStr);
            msg = apiCommonService.ocrWeixinTicket(dto);
        } catch (ApiException e) {
            msg = new ApiMessage(e.getCode(), e.getMessage());
        } catch (Exception e) {
            msg = new ApiMessage(ReqEnums.SYS_ERROR);
            e.printStackTrace();
        }
        return msg;
    }

    /**
     * orc识别url图片
     *
     * @param
     * @return
     */
    @PostMapping(value = "/ocrPictureByUrl")
    public ApiMessage ocrPictureByUrl(@RequestParam String paramStr) {
        ApiMessage msg;
        try {
            Dto dto= JsonUtils.toDto(paramStr);
            msg = apiCommonService.ocrPictureByUrl(dto);
        } catch (ApiException e) {
            msg = new ApiMessage(e.getCode(), e.getMessage());
        } catch (Exception e) {
            msg = new ApiMessage(ReqEnums.SYS_ERROR);
            e.printStackTrace();
        }
        return msg;
    }

    /**
     * 获取微信用户个人信息
     *
     * @param
     * @return
     */
    @PostMapping(value = "/getUserInfoByOpenId")
    public ApiMessage getUserInfoByOpenId(@RequestParam String paramStr) {
        ApiMessage msg;
        try {
            Dto dto= JsonUtils.toDto(paramStr);
            msg = apiCommonService.getUserInfoByOpenId(dto);
        } catch (ApiException e) {
            msg = new ApiMessage(e.getCode(), e.getMessage());
        } catch (Exception e) {
            msg = new ApiMessage(ReqEnums.SYS_ERROR);
            e.printStackTrace();
        }
        return msg;
    }

    /**
     * shopId获取商户信息
     *
     * @param
     * @return
     */
    @PostMapping(value = "/getShopById")
    public ApiMessage getShopById(@RequestParam String paramStr) {
        ApiMessage msg;
        try {
            Dto dto= JsonUtils.toDto(paramStr);
            msg = apiShopService.getShopById(dto);
        } catch (ApiException e) {
            msg = new ApiMessage(e.getCode(), e.getMessage());
        } catch (Exception e) {
            msg = new ApiMessage(ReqEnums.SYS_ERROR);
            e.printStackTrace();
        }
        return msg;
    }


    /**
     * 空回调
     *
     * @param
     * @return
     */
    @GetMapping("/tradeCallBack")
    public void tradeCallBack(HttpServletRequest request,HttpServletResponse response) {
        try {
            InputStream inStream = request.getInputStream();
            ByteArrayOutputStream outSteam = new ByteArrayOutputStream();
            byte[] buffer = new byte[1024];
            int len = 0;
            while ((len = inStream.read(buffer)) != -1) {
                outSteam.write(buffer, 0, len);
            }
            System.out.println("=================交易回调成功===============");
            outSteam.close();
            inStream.close();
            String result = new String(outSteam.toByteArray(), "GBK");
            //log.info("=======交易回调内容：" + result);
            System.out.println("=====交易回调内容:"+result);

        } catch (ApiException e) {

        } catch (Exception e) {
            e.printStackTrace();
        }
        Dto dto = Dtos.newDto();
        dto.put("tranCode", "0");
        dto.put("tranTime","20180827152138");
        dto.put("traceNo","000032");
        dto.put("rrn","131119510096");
        dto.put("orderId","20170312871231279813917931");
        dto.put("merchantId","900102689990000");
        dto.put("merchantName ","测试商户");
        dto.put("amount","16.1");
        dto.put("settleAmount","15.75");
        dto.put("ext_data","");
        dto.put("responseCode","00");
        dto.put("responseDesc","通知成功");
        dto.put("sign","4FAB9B00773793AEE0530100007FE168");

        //转为json
        String jsonMap = JSON.toJSONString(dto,true);
        response.setContentType("application/json;charset=GBK");
        PrintWriter out = null;
        try {
            out = response.getWriter();
        } catch (IOException e) {
            e.printStackTrace();
        }
        out.print(jsonMap);//返回json数值
        out.flush();
        out.close();



    }

    /**
     * 大奖公告
     * @param paramStr
     * @return
     */
    @PostMapping(value = "/getBigRewardList")
    public ApiMessage getBigRewardList(@RequestParam String paramStr) {
        ApiMessage msg;
        try {
            Dto dto= JsonUtils.toDto(paramStr);
            msg = apiBigRewardService.queryList(dto);
        } catch (ApiException e) {
            msg = new ApiMessage(e.getCode(), e.getMessage());
        } catch (Exception e) {
            msg = new ApiMessage(ReqEnums.SYS_ERROR);
            e.printStackTrace();
        }
        return msg;
    }




    /**
     * 空回调
     *
     * @param
     * @return
     */
    @PostMapping("/tradeCallBack")
    public void tradeCallBackPost(HttpServletRequest request,HttpServletResponse response) {
        tradeCallBack(request,response);
    }
}
